package cn.edu.xmu.oomall.customer.util;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;

/**
 * @author Jiazhe Yuan
 * @time 2021-12-23 21:01:59
 * @info 这个类用于处理openfeign的异常返回
 */
public class FeignExceptionHandler {
    private String errMessage;

    public FeignExceptionHandler(String errMessage) {
        this.errMessage = errMessage;
    }

    public ReturnObject onFeignException() {
        int index1 = errMessage.lastIndexOf('"');
        int index2 = errMessage.lastIndexOf('"', index1 - 1);
        int index3 = errMessage.lastIndexOf(':', errMessage.lastIndexOf(':') - 1);
        String errno = errMessage.substring(index3 + 1, index3 + 4);
        String errmsg = errMessage.substring(index2 + 1, index1);
        return new ReturnObject(ReturnNo.getByCode(Integer.parseInt(errno)), errmsg);
    }
}
